package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.workCellDataSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.common.enums.StatusEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.CellDataSetMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.workCellDataSet.JsmIWorkCellDataSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/1/11.
 */

public class JsmWorkCellDataSetServiceImpl implements JsmIWorkCellDataSetService {


    @Autowired
    private CellDataSetMapper cellDataSetMapper;


    /**
     * 加载表格数据
     * cellDataSet
     */
    @Override
    public JSONObject loadWorkCellDataSetTable(CellDataSet cellDataSet) throws Exception {
        JSONObject jsonObject = new JSONObject();
        List<CellDataSet> cellDataSets = cellDataSetMapper.queryTableList(cellDataSet);//查询表格数据
        jsonObject.put("rows", JSON.toJSON(cellDataSets));
        return jsonObject;
    }

    /**
     * 加载新增页面列表
     * @return
     */
    @Override
    public JSONObject loadWorkCellDataSetAddTable(CellDataSet cellDataSet) throws Exception {
        /*装载查询条件*/
        JSONObject jsonObject = new JSONObject();
        /* 进行分页查询分离 */
        List<CellDataSet> dataSets = cellDataSetMapper.queryAddList(cellDataSet);
        if (dataSets!=null) {
            jsonObject.put("total", dataSets.size());
            jsonObject.put("rows", JSON.toJSON(dataSets));
        }
        //返回结果
        return jsonObject;
    }



    /**
     * 保存关联
     * @param cellDataSetVo 新增数据封装类
     */
    @Override
    public Integer saveWorkCellDataSet(CellDataSetVo cellDataSetVo, EmployeeVo employee) throws Exception {
        Integer resInt = 0 ;

        List<CellDataSet> cellDataSets = JSON.parseArray(cellDataSetVo.getRowInfo(),CellDataSet.class);
        if(cellDataSets!=null){
            for(CellDataSet cs : cellDataSets){
                cs.setCellId(cellDataSetVo.getCellId());//set操作单元id
                cs.setDatasetCellId(UniqueUtil.uuid());//set主键ID
                cs.setCreatorId(employee.getEmployeeId());//set创建人id
                cs.setCreateEtime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                cs.setModifierId(employee.getEmployeeId());//set修改人ID
                cs.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));//set修改时间
                cs.setStatus(StatusEnum.Status_0.code());//set该条数据状态:0为使用,为删除
                cs.setEntityTypeId(cellDataSetVo.getEntityTypeId());//set所属生产生产实体类型
                resInt = resInt +  cellDataSetMapper.insertSelective(cs);//执行新增操作
            }
        }
        return resInt ;
    }


    /**
     * 根据ID修改数据关联记录为删除
     *
     * @param ids 主键id
     */
    @Override
    public void delWorkCellDataSet(String ids,EmployeeVo employee) throws Exception {
        this.updateStatusById(ids, StatusEnum.Status_1.code(),employee);//执行修改操作
    }

    /**
     * 操作单元 或者 一站一案
     * 数据集配置版本复制成新的版本
     *
     * @param version    当前新增版本信息
     * @param isWorkCell 是否是操作单元 或者 一站一案
     * @param oldVersion
     * @oldVersion 当前操作单元  或者 一站一案在用版本号
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void cloneDataSet(Version version, boolean isWorkCell, BigDecimal oldVersion,EmployeeVo employee) {
        String creatorId = employee.getEmployeeId();
        //查询当前版本为空的数据集
        CellDataSet cellDataSet=new CellDataSet();
        cellDataSet.setCellId(version.getRelatedId());
        List<CellDataSet> cellDataSets = cellDataSetMapper.queryDataSetTable(cellDataSet);
        if(cellDataSets!=null&&cellDataSets.size()>0){
            for(CellDataSet dataSet:cellDataSets){
                dataSet.setModifierId(creatorId);
                dataSet.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                dataSet.setVersion(version.getVersionNumber());
                cellDataSetMapper.updateByPrimaryKeySelective(dataSet);
            }
        }else{
            //克隆上个版本的数据
            cellDataSet.setVersion(oldVersion);
            List<CellDataSet> oldCellDataSets = cellDataSetMapper.queryDataSetTable(cellDataSet);
            if(oldCellDataSets!=null&&oldCellDataSets.size()>0){
                for(CellDataSet dataset:oldCellDataSets){
                    dataset.setDatasetCellId(UniqueUtil.uuid());
                    dataset.setCreatorId(creatorId);
                    dataset.setModifierId(creatorId);
                    dataset.setCreateEtime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                    dataset.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                    dataset.setVersion(version.getVersionNumber());
                    cellDataSetMapper.insertSelective(dataset);
                }
            }
        }

    }

    /**
     * 根据id修改状态公用方法
     *
     * @param ids    主键id
     * @param status 数据状态:0为使用1为删除
     * @return
     */
    private Integer updateStatusById(String ids, Short status,EmployeeVo employee) throws Exception {
        JSONArray temp_ids = JSONObject.parseArray(ids);
        /**装载条件*/
        Map map = new HashMap<>();
        map.put("modifyId", employee.getEmployeeId());//set修改人
        map.put("modifyTime", DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));//set修改时间
        map.put("status", status);//set该条数据状态，0为使用1为删除
        map.put("ids", temp_ids);//set主键ID
        return cellDataSetMapper.updateStatusById(map);//执行修改操作
    }
}
